package steveshrader.budget.client.dto;

import java.util.Date;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Expense implements IsSerializable {
        private Long id;
        private Date date;
        private Integer amount;                 //amount stored as Integer $5.00 -> 500
        private String displayAmount;   //displayAmount stored as String 5.00 -> $5.00
        private String vendor;
        private String expenseType;
        private String paymentType;

        @SuppressWarnings("unused")
        private Expense() {}    //needed for GWT RPC serialization

        /**
         * called on the server when putting model.Expense into dto for sending to the client
         * @param amount - passed in as Integer in the format 500 which will also be stored internally as String 5.00
         */
        public Expense(Long id, Date date, Integer amount, String vendor, String expenseType, String paymentType) {
                this.id = id;
                this.date = date;
                this.amount = amount;
                this.displayAmount = convertAmountToDisplay(amount);
                this.vendor = vendor;
                this.expenseType = expenseType;
                this.paymentType = paymentType;
        }
       
        /**
         * called on the client when sending data to the server
         * @param displayAmount - passed in as String in the format 5.00 which will also be stored internally as Integer 500
         */
        public Expense(Date date, String displayAmount, String vendor, String expenseType, String paymentType) {
                this.date = date;
                this.amount = convertDisplayToAmount(displayAmount);
                this.displayAmount = displayAmount;
                this.vendor = vendor;
                this.expenseType = expenseType;
                this.paymentType = paymentType;
        }
       
        public static String convertAmountToDisplay(Integer amount) {
                StringBuilder conversion = new StringBuilder("" + amount);
                while(conversion.length() < 3) {        //less than 1 dollar was passed in so prepend 0 until the dollar spot is filled
                        conversion.insert(0, "0");
                }
                conversion.insert(conversion.length() - 2, ".");
                return conversion.toString();
        }
       
        public static Integer convertDisplayToAmount(String displayAmount) {
                if(displayAmount.length() < 3) {        //money with no decimal should not be allowed use 0
                        return 0;
                }
                StringBuilder conversion = new StringBuilder(displayAmount);
                conversion.deleteCharAt(displayAmount.length() - 3);
                return Integer.parseInt(conversion.toString());
        }
       
        public Long getId() {
                return id;
        }
               
        public Date getDate() {
                return date;
        }

        public Integer getAmount() {
                return amount;
        }

        public String getDisplayAmount() {
                return displayAmount;
        }
       
        public String getVendor() {
                return vendor;
        }

        public String getExpenseType() {
                return expenseType;
        }

        public String getPaymentType() {
                return paymentType;
        }
}